package guoanmaker.operator.business.dao;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import guoanmaker.operator.business.model.OperatorServerClass;
import guoanmaker.operator.business.modelnobind.PendAudit;
import guoanmaker.operator.business.repository.OperatorServerClassRepository;

@Repository
public class PentAuditinfoDao {
	@PersistenceContext
	EntityManager em;
	
	@Autowired
	private OperatorServerClassRepository  operatorServerClassRepository;
	
	public List<OperatorServerClass> selectById(String pids) {
		List<OperatorServerClass> lists = null;
		if (StringUtils.isNotEmpty(pids)) {
			if (!pids.equals("")) {
				String[] commoditySs = pids.split(",");
				@SuppressWarnings("rawtypes")
				List twoIdList = new ArrayList();
				Collections.addAll(twoIdList, commoditySs);
				lists = operatorServerClassRepository.findByIdIn(twoIdList);
			}
		}
		return lists;
	}
	
	public Map<String,Object>   selectUserinfosPage(Integer page,Integer size,Integer status){
		String jpql="select new guoanmaker.operator.business.modelnobind.PendAudit(b.id,b.busNumber,b.companyName,b.commoditys,u.username,"
			+"u.applyForProviderTime,b.refusedtime,b.processtime) from  ProviderBusiness b left join b.personalUser u where 1=1 ";
		if(status!=null){
			jpql+=" and u.isProvider ='" + status + "' " ;
		}
		Query query = em.createQuery(jpql);
		List<PendAudit> result = query.getResultList();
		Map<String,Object>  map=new HashMap<>();
		map.put("totle", result.size());
		query.setFirstResult(page*size);
		query.setMaxResults(size);
		List<PendAudit> result1 = query.getResultList();
		for (PendAudit pendAudit : result1) {
			// 业务类别
			String str = pendAudit.getCommoditys();
			if(str!=null&&!str.equals("")){
				String servername = "";
    			List<OperatorServerClass> servers=	selectById(str);
    			if(servers.size()>0&&servers!=null){
    				for (OperatorServerClass operatorServerClass : servers) {
    					servername += operatorServerClass.getServerName()+",";
    				}
    			}
    			if(servers==null){
    				 servername = "";
    			}
				pendAudit.setCommoditys(servername);
			}
		}
		map.put("result", result1);
		return  map;
	};
}
